﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YLW.Plat.Jhfw.HL7V3.XSDModels.QUMT_IN020040UV01_Sspb;
using YLW.Plat.Jhfw.Model.JhfwSspb;

namespace YLW.Plat.Jhfw.HL7V3.V3Message
{
    public class V3_QUMT_IN020040UV01_Sspb
    {
        public static string OperationScheduleInfoQuery_Response(GetJhfwSspbListOut jsonData, string id_extension, string sender_name, string receiver_name)
        {
            string hl7Xml = string.Empty;

            if (jsonData == null)
            {
                return hl7Xml;
            }

            //根据XML XSD构建HL7 XML消息格式

            //创建根对象
            var xmlRoot = new QUMT_IN020040UV01
            {
                ITSVersion = "XML_1.0",
                id = new id { root = "2.16.156.10011.2.5.1.1", extension = Guid.NewGuid().ToString() },
                creationTime = new creationTime { value = DateTime.Now.ToString("yyyyMMddHHmmss") },
                interactionId = new interactionId { root = "2.16.156.10011.2.5.1.2", extension = "QUMT_IN020040UV01" },
                processingCode = new processingCode { code = "P" },
                processingModeCode = new processingModeCode(),
                acceptAckCode = new acceptAckCode { code = "AL" },
                receiver = new receiver
                {
                    typeCode = "RCV",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id = new id
                        {
                            root = "2.16.156.10011.2.5.1.3",
                            extension = receiver_name
                        }
                    }
                },
                sender = new sender
                {
                    typeCode = "SND",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id = new id
                        {
                            root = "2.16.156.10011.2.5.1.3",
                            extension = sender_name
                        }
                    }
                },
                acknowledgement = new acknowledgement
                {
                    typeCode = jsonData.subcode == "success" ? "AA" : "AE",
                    targetMessage = new targetMessage
                    {
                        id = new id
                        {
                            root = "2.16.156.10011.2.5.1.1",
                            extension = id_extension
                        }
                    },
                    acknowledgementDetail = new acknowledgementDetail
                    {
                        text = new text
                        {
                            value = jsonData.submsg
                        }
                    }
                },
                controlActProcess = new controlActProcess
                {
                    classCode = "CACT",
                    moodCode = "EVN",

                    queryAck = new queryAck
                    {
                        queryResponseCode = new queryResponseCode { code = jsonData.subcode == "success" ? "OK" : "error" },
                    }
                },
            };

            List<subject> subList = new List<subject>();

            if (jsonData != null && jsonData.data != null)
            {
                foreach (var listItem in jsonData.data)
                {
                    subList.Add(new subject
                    {
                        typeCode = "SUBJ",
                        actAppointment = new actAppointment
                        {
                            classCode = "ACT",
                            moodCode = "APT",
                            id = new id { root = "2.16.156.10011.2.5.1.33", extension = listItem.pbid },
                            effectiveTime = new effectiveTime
                            {
                                originalText = new originalText
                                {
                                    value = ""
                                },
                                low = new low
                                {
                                    value = listItem.yysssj
                                },
                                width = new width
                                {
                                    unit = listItem.yjssscdw,
                                    value = listItem.yjsssc
                                }
                            },
                            subject = new subject
                            {
                                typeCode = "SBJ",
                                patient = new patient
                                {
                                    classCode = "PAT",
                                    id = new id
                                    {
                                        item = new item[]
                                        {
                                            new item
                                            {
                                                root="2.16.156.10011.2.5.1.5",
                                                extension=""
                                            },
                                            new item
                                            {
                                                root="2.16.156.10011.1.11",
                                                extension=""
                                            },
                                            new item
                                            {
                                                root="2.16.156.10011.1.12",
                                                extension=""
                                            },
                                            new item
                                            {
                                                root="2.16.156.10011.2.5.1.4",
                                                extension=""
                                            },
                                            new item
                                            {
                                                root="2.16.156.10011.2.5.1.8",
                                                extension=listItem.series
                                            },
                                            new item
                                            {
                                                root="2.16.156.10011.2.5.1.9",
                                                extension=listItem.jzlsh
                                            }
                                        }
                                    },
                                    patientPerson = new patientPerson
                                    {
                                        name = new name
                                        {
                                            item = new item
                                            {
                                                part = new part { value = listItem.pname }
                                            }
                                        }
                                    }
                                }
                            },
                            reusableDevice = new reusableDevice
                            {
                                typeCode = "RDV",
                                manufacturedDevice = new manufacturedDevice
                                {
                                    classCode = "MANU",
                                    manufacturedDevice1 = new manufacturedDevice
                                    {
                                        classCode = "DEV",
                                        determinerCode = "INSTANCE",
                                        id = new id { extension = listItem.sbdm },
                                        name = new name
                                        {
                                            item = new item
                                            {
                                                part = new part
                                                {
                                                    value = listItem.sbmc
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            performer = new performer[]
                            {
                                new performer
                                {
                                    typeCode="PRF",
                                    assignedPerson=new assignedPerson
                                    {
                                        classCode="ASSIGNED",
                                        id=new id
                                        {
                                            item=new item[]
                                            {
                                                new item{ root="2.16.156.10011.1.4", extension=listItem.zdysdm }
                                            }
                                        },
                                        assignedPerson1=new assignedPerson
                                        {
                                            determinerCode="INSTANCE",
                                            classCode="PSN",
                                            name=new name
                                            {
                                                item=new item
                                                {
                                                    part=new part
                                                    {
                                                        value=listItem.zdysxm
                                                    }
                                                }
                                            }
                                        }
                                    }
                                },
                                new performer
                                {
                                    typeCode="PRF",
                                    assignedPerson=new assignedPerson
                                    {
                                        classCode="ASSIGNED",
                                        id=new id
                                        {
                                            item=new item[]
                                            {
                                                new item{ root="2.16.156.10011.1.4", extension=listItem.zsdm }
                                            }
                                        },
                                        assignedPerson1=new assignedPerson
                                        {
                                            determinerCode="INSTANCE",
                                            classCode="PSN",
                                            name=new name
                                            {
                                                item=new item
                                                {
                                                    part=new part
                                                    {
                                                        value=listItem.zsxm
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            location = new location
                            {
                                typeCode = "LOC",
                                serviceDeliveryLocation = new serviceDeliveryLocation
                                {
                                    classCode = "SDLOC",
                                    id = new id
                                    {
                                        item = new item[]
                                        {
                                            new item{ root="2.16.156.10011.1.26", extension=listItem.zxksdm }
                                        }
                                    },
                                    addr = new addr
                                    {
                                        item = new item
                                        {
                                            part = new part
                                            {
                                                value = listItem.zxksmc
                                            }
                                        }
                                    },
                                    location = new location
                                    {
                                        classCode = "LOC",
                                        determinerCode = "INSTANCE",
                                        name = new name
                                        {
                                            item = new item
                                            {
                                                part = new part
                                                {
                                                    value = listItem.ssj
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            referencedOrder = new referencedOrder
                            {
                                typeCode = "OREF",
                                actOrder = new actOrder
                                {
                                    classCode = "ACT",
                                    moodCode = "RQO",
                                    id = new id
                                    {
                                        root = "2.16.156.10011.1.24",
                                        extension = listItem.sqdh
                                    }
                                }
                            },
                            extension = new extension
                            {
                                sjly = new value { value1 = listItem.sjly },
                                lyxt = new value { value1 = listItem.lyxt },
                                hoscode = new value { value1 = listItem.hoscode },
                                pbid = new value { value1 = listItem.pbid },
                                jzlsh = new value { value1 = listItem.jzlsh },
                                sqdh = new value { value1 = listItem.sqdh },
                                mrn = new value { value1 = listItem.mrn },
                                pname = new value { value1 = listItem.pname },
                                series = new value { value1 = listItem.series },
                                stay = new value { value1 = listItem.stay },
                                yysssj = new value { value1 = listItem.yysssj },
                                zdysdm = new value { value1 = listItem.zdysdm },
                                zdysxm = new value { value1 = listItem.zdysxm },
                                zsdm = new value { value1 = listItem.zsdm },
                                zsxm = new value { value1 = listItem.zsxm },
                                ssj = new value { value1 = listItem.ssj },
                                yjsssc = new value { value1 = listItem.yjsssc },
                                yjssscdw = new value { value1 = listItem.yjssscdw },
                                zxksdm = new value { value1 = listItem.zxksdm },
                                zxksmc = new value { value1 = listItem.zxksmc },
                                sbdm = new value { value1 = listItem.sbdm },
                                sbmc = new value { value1 = listItem.sbmc },
                                useflag = new value { value1 = listItem.useflag },
                                in_empid = new value { value1 = listItem.in_empid },
                                in_date = new value { value1 = listItem.in_date },
                                xg_empid = new value { value1 = listItem.xg_empid },
                                xgsj = new value { value1 = listItem.xgsj }
                            }
                        }
                    });
                }
            }

            xmlRoot.controlActProcess.subject = subList.ToArray();

            hl7Xml = HL7V3Helper.SerializeToXml(xmlRoot);

            return hl7Xml;
        }
    }
}
